//  Created by <Omar MALAGOUEN>

//  Copyright 2011 All rights reserved.
//

#ifndef CRIPDVD_H
#define CRIPDVD_H


#include <QDialog>
#include <QFile>
#include <QThread>
#include <QDir>
#include <QTreeView>
#include <QFileDialog>
#include <QSettings>
#include <QProcess>
#include <QTime>
#include <QTextStream>
#include <QProgressDialog>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QMessageBox>

#include "cdvdtreeitem.h"
#include "common.h"
#include "cdvdtreemodel.h"


namespace Ui {
    class CRipDVD;
}


class c_title {

public:
    c_title(QString DVDWorkingDirectory, int title_number, QString infoVideoDVD);

public:
    QString DVDWorkingDirectory;                        // d:/dvdrip
    QString vob_file;					// video-title-5.vob
    QString profile;					// video-title-5.profile
    bool enabled;
    QString info_file_of_title;                         // dvd-5.txt
    QString chapter_filename;                           // chapters-title-5.txt
    int title_number;					// 5

    QString id_filename;				// dvd://5
    QString id_demuxer;					// mpegps
    QString id_video_format;                            // 0x10000002
    double id_video_bitrate;                            // 9800000
    double id_video_width;				// 720
    double id_video_height;				// 576
    double id_video_fps;				// 25.000
    double id_video_aspect;				// 1.3333
    double id_audio_format;				// 65537
    double id_audio_bitrate;                            // 1536000
    int id_audio_rate;					// 48000
    int id_audio_nch;					// 2
    double id_length;					// 502.56
    double id_seekable;					// 1
    int number_of_chapters;				// 3
    int number_of_audio;				// 3
    int number_of_subtitle;				// 12

    QList<class c_subtitle> list_subtitle;
    QList<class c_audio>    list_audio;
    QList<class c_chapter>  list_chapter;

    bool appendToListAudio(c_audio);
    bool appendToListSubtitle(c_subtitle);
    bool appendToListChapter(c_chapter);
};

class c_chapter {
public:
    //c_chapter(QString DVDWorkingDirectory, int title_number, QString info);
    c_chapter();
public:
    QString cmdCreateChapter;                                   // mplayer bla bla
    bool enabled;
    QString chapter_name;
    int title_number;
    int number;
    QTime beginQTime;
    QString beginQString;
    QString DVDWorkingDirectory;                                  // d:/dvdrip
};

class c_audio {

public:
    //c_audio(QString DVDWorkingDirectory, int title_number, int aid, QString info);
    c_audio();
public:
    bool enabled;
    QString DVDWorkingDirectory;                                  // d:/dvdrip
    QString cmdCreateAudio;                                     // mplayer bla bla
    QString lang;
    int title_number;
    int aid;
    QString id_audio_codec;                                     // dvdpcm
    QString id_video_codec;                                     // mpegpes
    QString audio_file_name;                                    // video-title-1-aid-129-lang-en.ac3
    QString format;      // dts, lpcm, ...
    QString format2;     // 5.1 or stereo
    int streamNumber; //0 or 1 or ....
};

class c_subtitle {
public:
    c_subtitle(QString DVDWorkingDirectory, int title_number, int sid, QString info);
public:
    bool enabled;
    QString DVDWorkingDirectory;                                  // d:/dvdrip
    QString cmdCreateSubtitle;                                  // mplayer bla bla
    QString lang;
    int title_number;
    int sid;
    QString idx_file_name;                                      // video-title-1-sid-7-lang-el.idx
    QString sub_file_name;                                      // video-title-1-sid-7-lang-el.sub
};

class CRipDVD : public QDialog
{
    Q_OBJECT

public:
    explicit CRipDVD(QWidget *parent = 0);
    ~CRipDVD();


    void scanDVDTitle();
    void changeCheckState(QTreeWidgetItem *, int);

    QSettings *settings; // Application conf file
    QString allInfoDVD[1024];
    //char a_allInfoDVD[8][10240];

    int alreadyDone;
    QProcess *processGetInfo;
    QTime chrono;
    QString profileTitles;
    QString profileChapters;
    QString profileAudios;
    QString profileSubtitles;
    QString profileMkv;

    QString DVDWorkingDirectory;			// d:/dvdrip
    QString DVDPath;					// E:/ or c:/dvd.iso for example

    int numberOfTitleInDVD; 				// 10
    QString dvd_device; 				// "led zeppelin dvd2.iso"
    QString dvd_id; 					// 8a29109cacd2144afd20f3ef0023e6be
    QString dvd_volume_id; 				// led_zeppelin_2
    int start_cell;					// 0
    QString pack;					// 0x0-0x13
    int start;						// 0
    int end;						// 19
    QList<c_title> list_title;				//
    QList<QString> list_info_file;			// info-dvd-1.txt info-dvd-2.txt info-dvd-3.txt
    int number_of_angle;				// 1
    QList<QString> attachment;				// List of file to put on attachment of the mkv file,
    // Contain by default the ifo file of the DVD
    QString mkv_rip_filename;
    QString mkv_rip_dirname;

    int create_list_info_file_of_title();               // create all file info-dvd-${i}.txt in the directory DVDWorkingDirectory for i in 1 .. numberOfTitleInDVD
    void fill_list_title();				// create the list of title by using list_info_file
    void fill_tree_widget(c_title);                            // fill the tree widget
    void setInfoTitle(int );

protected:
    void changeEvent(QEvent *e);

private:
    Ui::CRipDVD *ui;

private slots:
    void on_startDVDButton_clicked();
    void on_pushButtonScan_clicked();
    void on_dvdWorkingDirectoryButton_clicked();
    void on_openISOButton_clicked();
    void on_openDVDButton_clicked();
    void on_lineEditDVDWorkingDirectory_textChanged(QString );
    void on_lineEditDVDPath_textChanged(QString );
    void on_cancelDVDButton_clicked();
    void readDVDInfo();
    void finishedScanning(int ,QProcess::ExitStatus );
    void treeWidgetClicked(QTreeWidgetItem *, int);

};

// retourne la chaine comprise en debut et fin dans str
// regexBetween("Alphonse brown is the best","brown","the") = " is "
QString regexBetween(QString str,QString reg,int log = 0);

#endif // CRipDVD_H
